<?php




define('SESSION_FILE','/var/session_sinapsis.log');
define('YOUR_EOL', "\n");
//require_once('/var/www/sinapsisvn/modules/rbac/db/dbinterface.php');



class session{



	public function Insertar_session($login,$roles){
                
		$logpath=SESSION_FILE;
                $id=$this->obtenerid();
                
	if (file_exists($logpath)){
		$file=fopen($logpath,'a');
		if ($file){
			$line=$id.' access: '.date('Y-m-d H:i:s').' username= '.$login.' role=';
			foreach($roles as $rol){
                            $line=$line.' '.$rol.' ';
                        }

                        fputs($file,$line);
                        fputs($file,"\r\n");
			fclose($file);
                        return $id;
		}
	}

		return false;

	}

        function obtenerid(){
            $logpath=SESSION_FILE;

            if (file_exists($logpath)){

                $file = $logpath;
                $data = file($file);
                $line = $data[count($data)-1];
                $pieces = explode(" ", $line);
                if(is_numeric($pieces[0]))
                    return (($pieces[0])+1);
                else
                    return 1;

            }
        }

	function Seleccionar_session($id){

            $logpath=SESSION_FILE;

            if (file_exists($logpath)){

                $file = $logpath;
                $data = file($file);
                    foreach($data as $line){
                        $pieces = explode(" ", $line);
                        if(($pieces[0])==$id)
                            return $line;
                    }
                    return FALSE;

            }
            return FALSE;


	}

	public function Eliminar_session($id){
            $logpath=SESSION_FILE;

            if (file_exists($logpath)){

                $file = $logpath;
                $data = file($file);
                $i=0;
                foreach($data as $line){
                    $pieces = explode(" ", $line);

                    if(($pieces[0])==$id)break;
                    $i++;

                }
                //var_dump($data[$i]);
                if ($i>=sizeof($data))return false;
                unset($data[$i]);
                $fp = fopen($logpath, 'w+');
                if($fp){
                    foreach($data as $line) { fwrite($fp,$line); }
                    fclose($fp);
                    return true;
                }

            }
            return FALSE;
		

	}
	
	public function Terminar_session($id){
            $logpath=SESSION_FILE;

            if (file_exists($logpath)){

                $file = $logpath;
                $data = file($file);
                $i=0;
                foreach($data as $line){
                    $pieces = explode(" ", $line);

                    if(($pieces[0])==$id)break;
                    $i++;

                }
                //var_dump($data[$i]);
                if ($i>=sizeof($data))return false;
                //var_dump($data[$i]);
                $data[$i]=substr($data[$i], 0, -1)."end access: ".date('Y-m-d H:i:s')."\n";
                $fp = fopen($logpath, 'w+');
                if($fp){
                    foreach($data as $line) { fwrite($fp,$line); }
                    fclose($fp);
                    return true;
                }

            }
            return FALSE;


	}



}
?>
